//
//  VehicleListView.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/6.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//

#import "VehicleListView.h"
#import "VehicleListModel.h"
#import "AreaDetailViewController.h"
#import "CarDetailListCell.h"
#import "MJRefresh.h"

@interface VehicleListView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *vehicleListTabelView;
@property (nonatomic,strong)NSMutableArray *dataListArray;
@property (nonatomic,assign)NSInteger index;
@property (nonatomic, strong) NSMutableArray *heightArray;
@property (nonatomic,strong)LoadNetView *loadNetView;
@end
static NSString *CarDetailListCellID = @"CarDetailListCellID";

@implementation VehicleListView
- (LoadNetView *)loadNetView
{
    if (!_loadNetView) {
        _loadNetView = [LoadNetView new];
        [self addSubview:_loadNetView];
        [_loadNetView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(self);
        }];
        _loadNetView.hidden = NO;
    }
    return _loadNetView;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self loadUI];
    }
    return self;
}
-(void)loadData
{
    _dataListArray = [NSMutableArray array];
    _index = 1;
    NSDictionary *param = @{
                            @"plateNum":@"",
                            @"pageIndex":@(_index),
                            @"pageSize":@15
                            };
    [self.loadNetView start];
    [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_QueryCarStatus method:@"POST" parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(id  _Nullable responseObject) {
        [_vehicleListTabelView.mj_header endRefreshing];
        [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
        if ([responseObject[@"state"] integerValue] == 0) {
            NSArray *useRegionsArr = responseObject[@"obj"][@"content"];
            if (useRegionsArr.count && useRegionsArr) {
                for (NSDictionary *dic in useRegionsArr) {
                    VehicleListModel *model = [VehicleListModel new];
                    [model yy_modelSetWithDictionary:dic];
                    [_dataListArray addObject:model];
                }
            }
            if (_dataListArray.count) {
                [_vehicleListTabelView reloadData];
            }
        }else{
        }
        
        
    } failure:^(NSError * _Nonnull error) {
        [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
        [_vehicleListTabelView.mj_header endRefreshing];
    }];
    
}
- (void)loadNewData
{
    [self loadData];
}
- (void)loadMoreData
{
    _index += 1;
    NSDictionary *param = @{
                            @"plateNum":@"",
                            @"pageIndex":@(_index),
                            @"pageSize":@15
                            };
    [self.loadNetView start];
    [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_QueryCarStatus method:@"POST" parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(id  _Nullable responseObject) {
        [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
        if ([responseObject[@"state"] integerValue] == 0) {
            NSArray *useRegionsArr = responseObject[@"obj"][@"content"];
            if (useRegionsArr.count && useRegionsArr) {
                for (NSDictionary *dic in useRegionsArr) {
                    VehicleListModel *model = [VehicleListModel new];
                    [model yy_modelSetWithDictionary:dic];
                    [_dataListArray addObject:model];
                }
            }
            if (_dataListArray.count) {
                [_vehicleListTabelView reloadData];
            }
        }else{
        }
        
        
    } failure:^(NSError * _Nonnull error) {
         [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
    }];
    [_vehicleListTabelView.mj_footer endRefreshing];

}
- (void)loadUI
{
    _vehicleListTabelView = [UITableView new];
    _vehicleListTabelView.delegate = self;
    _vehicleListTabelView.dataSource = self;
    _vehicleListTabelView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    _vehicleListTabelView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [_vehicleListTabelView registerClass:[CarDetailListCell class] forCellReuseIdentifier:CarDetailListCellID];;
    self.vehicleListTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:self.vehicleListTabelView];
    [_vehicleListTabelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-TabBarHeight);
        make.top.mas_equalTo(self.mas_top);
    }];
    
}

#pragma mark - UITableView 代理 数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataListArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CarDetailListCell *carDetaliCell = [tableView dequeueReusableCellWithIdentifier:CarDetailListCellID];
    if (_dataListArray.count) {
        carDetaliCell.model = _dataListArray[indexPath.row];
    }
    carDetaliCell.selectionStyle = UITableViewCellSelectionStyleNone;
    return carDetaliCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSNumber *cellHeight = [self.heightArray h_safeObjectAtIndex:indexPath.row];
    if (cellHeight) {
        NSLog(@"不用计算，直接返回行高了");
        return [cellHeight floatValue];
    }else{
        VehicleListModel *model = [VehicleListModel new];
        if (_dataListArray.count) {
            model = _dataListArray[indexPath.row];
        }
        NSString *status = model.position;
        CGFloat statusHeight = 0;
        if (!status) {
            statusHeight = 11;
        }else{
            statusHeight = [CarDetailListCell tableView:tableView rowHeightForObject:status];
        }
        CGFloat iconHeight = 149-11;
        [self.heightArray addObject:@(statusHeight + iconHeight)];
        NSLog(@"第一次加载计算一次，每次展示都计算一次");
        return statusHeight + iconHeight;
    }
}
- (NSMutableArray *)heightArray{
    if (!_heightArray) {
        _heightArray = @[].mutableCopy;
    }
    return _heightArray;
}
@end
